import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Service } from '../../server/server';
import { RolesDetailsComponent } from '../roles/roles-details/roles-details.component';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Pagination } from '../../public/serve/pcrudInfo';
import { PrivilegeTableComponent } from '../privilege/privilege-table/privilege-table.component';
import { TabVal } from './privilege-table/privilege-table.component';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import { defaultSearchType } from '../../public/serve/pcrudInfo';
declare var $: any;

@Component({
  selector: 'app-privilege',
  templateUrl: './privilege.component.html',
})
export class PrivilegeComponent implements OnInit {

  tableNumber: number = 0;

  right: any = {
    "user": {},
    "privilege": {}
  };

  constructor(private server: Service, private removeTableService: RemoveTableService, private cudTableService: CudTableService) {
    this.right = $.extend(this.right, server.getRight());
  }

  public isActive = this.server.isActive;
  ngOnInit() {
    this.searchData = {
      queryCriteria: '',
      queryType: 3,
      pageNumber: 1,
      pageSize: 20,
    };
    this.server.eventEmit.subscribe((value: any) => {
      this.isActive = value;
    });
  }

  ngDoCheck() {
    this.right = $.extend(this.right, this.server.getRight());
  }

  public tabVal: TabVal;
  public operationId: string;

  // 删除按钮
  isSelect: boolean = true;
  isSelectFromTableHandler(isSelect: boolean) {
    this.isSelect = isSelect;
  }

  // 禁用
  reduction() {
    setTimeout(() => {
      this.isSelect = true;
    }, 100);
  }

  //表格
  public totalItems: number;
  ParentList(totalItems: number) {
    this.totalItems = totalItems;
  }
  //新增
  public newly_added_result_code: string;
  ParentAdd(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, '0');
    $('#newlyAdded').modal('hide');
  }
  //详情
  public tabval: TabVal;
  ParentDetails(tabVal: TabVal) {
    this.tabVal = tabVal;
    //console.log(this.tabVal)
  }

  // 修改
  public edit_result_code: string;
  ParentUpdate(result_code: string) {
    this.table.getTabList(this.pageNumber, this.pageSize, "0");
  }

  //删除click
  public deletes() {
    if (this.removeTableService.getRemoveTableId.length == 0) {
      this.server.prompt = { prompt: true, success: false, fail: false, promptStatement: "请选中要删除的数据!" }
      this.server.promptclose;
    } else {
      $("#delete").modal('show');
    }
  }
  //删除
  public remove_result_code: string;
  ParentDelete(result_code: string) {
    this.table.getTabList(1, this.pageSize, "0");
  }

  //导入table
  @ViewChild("table")
  table: PrivilegeTableComponent;

  // 当前页（默认为第一页）
  public pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  public pageSize: number = 20;
  // id
  private id: string = '0';
  // 分页
  public pagination: Pagination;
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

    this.table.getTabList(this.pageNumber, this.pageSize, '0');
  }


  public searchData: defaultSearchType;
  //查询
  search() {
    let data = this.searchData = {
      queryCriteria: this.searchData.queryCriteria,
      queryType: 1,
      pageNumber: 1,
      pageSize: 20,
    }
    //console.log(data)
    let url = this.server.baseUrl+'admin/operations/getPageList';
    this.cudTableService.postRequest(url, data).then((data: any) => {
      //console.log(data.length)
      //this.tabVal = data.result;
      this.ParentList(data.length);
      this.server.privilegeeventEmit.emit(data.result);
    }).catch();
  }
  //响应搜索
  changesearch() {
    this.search();
  }
}
